package com.apcc4m.sfpushbatch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import com.apcc4m.sfpushbatch.reader.NotificationPagingItemReader;
import com.apcc4m.sfpushbatch.reader.PushStatusPagingItemReader;
import com.apcc4m.sfpushbatch.repository.Notification;
import com.apcc4m.sfpushbatch.repository.PushStatus;
import com.apcc4m.sfpushbatch.writer.NotificationItemWriter;
import com.apcc4m.sfpushbatch.writer.PushStatusItemWriter;

@Configuration
@EnableBatchProcessing
public class SfpushJobConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

//    /**
//     * 默认simpleJobLauncher的任务执行器是同步的，我们这里根据需要换成自己的执行器
//     * @param jobRepo
//     * @return
//     */
//    @Bean
//    public JobLauncher jobLauncher(){
//        System.out.println("init jobLauncher");
//        SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
//        simpleJobLauncher.setJobRepository(jobRepository);
//        SimpleAsyncTaskExecutor st = new SimpleAsyncTaskExecutor();
//        st.setConcurrencyLimit(10);
//        simpleJobLauncher.setTaskExecutor(st);
//        return simpleJobLauncher;
//    }

    /**
     * 注解bean名字不能和job名字一样,有时会注入不了
     * @return
     * @throws Exception
     */
    @Bean
    public Job notificationJob1() throws Exception {
        System.out.println("notificationJob1");
        return jobs.get("notificationJob")
                .start(notificationStep())
                .build();
    }

    /**
     * 注解bean名字不能和job名字一样,有时会注入不了
     * @return
     * @throws Exception
     */
    @Bean
    public Job pushStatusJob1() throws Exception {
        System.out.println("pushStatusJob1");
        return this.jobs.get("pushStatusJob")
                .incrementer(new RunIdIncrementer())
                .start(pushStatusStep())
                .build();
    }

    @Bean
    protected Step notificationStep() throws Exception {
        System.out.println("notificationStep");
        return this.steps.get("notificationStep")
                .<Notification,Notification>chunk(1)
                .reader(notificationReader())
                .writer(notificationWriter())
                .build();
    }

    @Bean
    protected Step pushStatusStep() throws Exception {
        System.out.println("pushStatusStep");
        return this.steps.get("pushStatusStep")
                .<PushStatus,PushStatus>chunk(10000)
                .reader(pushStatusReader())
                .writer(pushStatusWriter())
                .build();
    }
    
    @Bean
    public ItemReader<Notification> notificationReader(){
        NotificationPagingItemReader<Notification> reader = new NotificationPagingItemReader<Notification>();
        return reader; 
    }
    @Bean
    public ItemReader<PushStatus> pushStatusReader(){
        PushStatusPagingItemReader<PushStatus> reader = new PushStatusPagingItemReader<PushStatus>();
        return reader; 
    }
//    @Bean
//    public ItemProcessor<Notification, Notification> processor(){
//        NotificationItemProcessor processor = new NotificationItemProcessor();
//        return processor; 
//    }

    @Bean
    public ItemWriter<Notification> notificationWriter(){
        NotificationItemWriter<Notification> reader = new NotificationItemWriter<Notification>();
        return reader; 
    }
    @Bean
    public ItemWriter<PushStatus> pushStatusWriter(){
        PushStatusItemWriter<PushStatus> reader = new PushStatusItemWriter<PushStatus>();
        return reader; 
    }
}
